package cn.yunhe.servlet;

import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.Product;
import cn.yunhe.service.ProductService;
import cn.yunhe.util.JsonUtil;
import cn.yunhe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/GetProductByChildrenJsonServlet")
public class GetProductByChildrenJsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer epcid = 0;
        if (request.getParameter("epcid")!="") {
            epcid = Integer.parseInt(request.getParameter("epcid"));
        }


        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));

        ProductService productService = new ProductService();

        int pageSize= PageUtil.PAGE_SIZE;
        List<Product> productList = productService.getProductByChildClassId(pageIndex,pageSize,epcid);

        int totalRows = productService.getProductByChildClassIdCount(epcid);
        int totalPage = PageUtil.getTotalPages(totalRows);


        Map<String,Object> maps = new HashMap<String, Object>();
        maps.put("productList",productList);
        maps.put("pageIndex",pageIndex);
        maps.put("totalPages",totalPage);
        maps.put("totalRows",totalRows);

        JsonUtil.getJson(response,maps);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
